PROJECT = "AGV-MOTORS-CORE"
VERSION = "1.0.0"

--警告【测试版固件】

--[[GPIO define PWM
GPIO2-M2(右前)
GPIO10-M3(左后)
GPIO6-M1(左前)
GPIO8-M4(右后)
]]

--[[GPIO define direction
GPIO3-DIR1(左前)
GPIO7-DIR2(右前)
GPIO5-DIR3(左后)
GPIO4-DIR4(右后)
]]

--7ED273043254	本设备的BLE MAC地址

--hello lua!
ledflash_waiting=1
speed=100

gpio.setup(12,0)
gpio.setup(13,0)
gpio.setup(3,1)
gpio.setup(5,1)
gpio.setup(7,0)
gpio.setup(4,0)



pwm.open(2,0,20)
pwm.open(10,0,20)
pwm.open(6,0,20)
pwm.open(8,0,20)

sys = require "sys"--不管干啥，先写上这个
uart.setup(1)

sys.taskInit(function ()

    while true do
        sys.wait(10)
        if bledata=="nil" then
            situation="等待指令"
        end
        if bledata=="00" then
            situation="停止"
            ledflash_waiting=0
            gpio.set(12,1)
            gpio.set(13,1)
            pwm.close(2)
            pwm.close(10)
            pwm.close(6)
            pwm.close(8)
        end
        if bledata=="AA64" then
            situation="脉冲设置100HZ"
            speed=100
        end
        if bledata=="AAC8" then
            situation="脉冲设置200HZ"
            speed=200
        end
        if bledata=="AA12C0" then
            situation="脉冲设置300HZ"
            speed=300
        end
        if bledata=="AA1900" then
            situation="脉冲设置400HZ"--最多400，不然跑不动
            speed=400
        end


        if uartdata=="BBAA" then--前
            ledflash_waiting=0
            gpio.set(3,1)
            gpio.set(5,1)
            gpio.set(7,0)
            gpio.set(4,0)
            pwm.open(2,speed,20)
            pwm.open(10,speed,20)
            pwm.open(6,speed,20)
            pwm.open(8,speed,20)
        end
        if uartdata=="BBBB" then--后
            ledflash_waiting=0
            gpio.set(3,0)
            gpio.set(5,0)
            gpio.set(7,1)
            gpio.set(4,1)
            pwm.open(2,speed,20)
            pwm.open(10,speed,20)
            pwm.open(6,speed,20)
            pwm.open(8,speed,20)
        end
        if uartdata=="BBCC" then
            --"向左移动"
            ledflash_waiting=0
            gpio.set(3,1)
            gpio.set(5,0)
            gpio.set(7,1)
            gpio.set(4,0)
            pwm.open(2,speed,20)
            pwm.open(10,speed,20)
            pwm.open(6,speed,20)
            pwm.open(8,speed,20)
        end
        if uartdata=="BBDD" then
            --"向右移动"
            ledflash_waiting=0
            gpio.set(3,0)
            gpio.set(5,1)
            gpio.set(7,0)
            gpio.set(4,1)
            pwm.open(2,speed,20)
            pwm.open(10,speed,20)
            pwm.open(6,speed,20)
            pwm.open(8,speed,20)

        end
        if uartdata=="BBEE" then
            --"顺时针转动"
            ledflash_waiting=0
            gpio.set(3,1)
            gpio.set(5,1)
            gpio.set(7,1)
            gpio.set(4,1)
            pwm.open(2,100,20)
            pwm.open(10,200,20)
            pwm.open(6,200,20)
            pwm.open(8,100,20)
        end
        if uartdata=="BBFF" then
            --"逆时针转动"
            ledflash_waiting=0
            gpio.set(3,0)
            gpio.set(5,0)
            gpio.set(7,0)
            gpio.set(4,0)
            pwm.open(2,200,20)
            pwm.open(10,100,20)
            pwm.open(6,100,20)
            pwm.open(8,200,20)
        end




    end

end)


sys.taskInit(function ()--等待指令时的闪灯
    while true do
        sys.wait(20)
        if ledflash_waiting==1 then
            gpio.set(12,1)
            gpio.set(13,1)
            sys.wait(100)
            gpio.set(12,0)
            gpio.set(13,0)
            sys.wait(2000)
        end
    end

end)

sys.taskInit(function ()--行动时的闪灯
    while true do
        sys.wait(20)
        if ledflash_waiting==0 then
            gpio.set(12,1)
            gpio.set(13,1)
            sys.wait(300)
            gpio.set(12,0)
            gpio.set(13,0)
            sys.wait(1200)
        end
    end

end)


uart.on(1, "receive", function(id, len)
    uartdata = uart.read(id, len)
    bledata=uartdata
end)



sys.run()
